// pages/controlBooth/controlBooth.js
var app = getApp();
var url = app.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    alarmNum: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 页面显示
    // var deptName = wx.getStorageSync('deptName')
    // if (deptName === "" || deptName == null) {
    //   wx.setNavigationBarTitle({
    //     title: "控制台",
    //   })
    // } else {
    //   wx.setNavigationBarTitle({
    //     title: deptName,
    //   })
    // }
    this.checkLogin()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //this.setData({ alarmNum: 0 })
    //this.checkLogin()

  },

  checkLogin: function () {
    let name = app.globalData.login_name;
    //console.log(name, "name");

    let userid = app.globalData.userid;
    //console.log(userid, "userid");
    
    this.queryAlarm();
    // var name = wx.getStorageSync('userName')
    // if (name == '' || name == null) {
    //   wx.showModal({
    //     title: '提示',
    //     content: "用户未登录",
    //     confirmText: '去登录',
    //     showCancel: true,
    //     success: function (res) {
    //       if (res.confirm) {
    //         wx.navigateTo({
    //           url: '../login/login',
    //         })
    //       } else if (res.cancel) {
    //         wx.navigateTo({
    //           url: '../index/index',
    //         })
    //       }
    //     }

    //   })
    // } else {
    //   this.queryAlarm()
    // }
  },
  queryAlarm: function () {
    var that = this

    var header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    };

    wx.request({
      //url: app.globalData.url+"GetUnHandleBranchAlarmSize",
      url:app.globalData.url+'GetWXBranchAlarmSize',
      data:{
        userid:app.globalData.userid,       
      },    
      type:"post",
      // data:{
      //   userid:app.globalData.userid,
      //   high: true,
      //   low: true
      // },
      success:res=>{
        //{"online_branch_devs":"[\"002502A701\",\"002502A702\",\"002502A703\",\"002502A704\",\"002502BF01\",\"002502BF02\",\"002502BF03\",\"002502BF04\",\"002502BF00\",\"002502BE01\",\"002502BE02\",\"002502BE03\",\"002502BE04\",]
        //auto_count":"1","manual_size":"63","unusual_TurnOff":"[]","unusual_TurnOff_size":"0","unusual_TurnOn_size":"0","online_single":"0","online_branch":"1007","unusual_TurnOn":"[]","alarm_size":"9786","auto_size":"1","online_single_devs":"[]","single_size":"64","branch_size":"1117"}
        //{"size":0}
       
        let sizenum = res.data.size;
        that.setData({
          alarmNum:sizenum
        });
      },
      fail:error=>{
      }
    })



    // var header = { 'content-type': 'application/x-www-form-urlencoded' }
    // var date = new Date();
    // let year = date.getFullYear()
    // let mon = date.getMonth() + 1
    // let day = date.getDate()
    // var dateStr = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day)
    // var params = { bdate: dateStr, edate: dateStr }
    // var encodingStr = app.ecbEncrypto(params)
    // wx.request({
    //   url: url + '/xywl/appAlarmInfo.htm',
    //   data: encodingStr,
    //   method: 'POST',
    //   header: header,
    //   success: function (res) {
    //     var decodingStr = app.ecbDecrypto(res.data)
    //     var rest = JSON.parse(decodingStr)
    //     console.log(rest)
    //     if (rest.res == 0) {
    //       var deptId = wx.getStorageSync('deptId')
    //       var dataList = rest.dataList
    //       var count = 0;
    //       for (var i = 0; i < dataList.length; i++) {
    //         if (dataList[i].PARENTID == deptId || deptId == 1) {
    //           count++
    //         }
    //       }
    //       that.setData({ alarmNum: count })
    //     }
    //   },
    //   complete: function () {
    //     wx.stopPullDownRefresh()
    //   }
    // })
  },
  bindToQuery: function () {
    wx.navigateTo({
      url: '../control/query',
    })
  },
  bindToReadout: function () {
    wx.navigateTo({
      url: '../control/readout',
    })
  },
  bindToMap: function () {
    wx.navigateTo({
      url: '../simple_map/simple_map',     
    })
     
    // wx.switchTab({
    //   url: '../index/index',
    // })
  },
  bindToKeep: function () {
    wx.navigateTo({
      url: '../keepLight/keepLight',
    })
  },
  bindToMonitor: function () {
    wx.navigateTo({
      //url: '../monitor/monitor',
      url:'../alarm_history/alarm_history'
    })
  },
  bindEXception: function () {
    wx.navigateTo({
      url: '../abnormallyReport/abnormallyReport',
    })
  },
  bindEXceptionList: function () {
    wx.navigateTo({
      url: '../reportList/reportList',
    })
  },
  bindToZdSet: function () {
    wx.navigateTo({
      url: '../zdSetup/zdSetup',
    })
  },

  switchuser:function(){
    wx.setStorageSync('username', "")
    wx.setStorageSync('password', "")
    wx.setStorageSync('userid', "")
    wx.navigateTo({
      url: '../login/login',
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})